<?php
/**
 * Created by PhpStorm.
 * User: ZhangYinxian
 * Date: 2017-03-26
 * Time: 16:14
 */

namespace app\index\controller;


use think\Controller;
use think\facade\Session;
use think\Db;

class User extends Controller
{
    public function login(){
        $username = input('username');
        $password = input('password');
        if ($username==null || $password==null){
            $result =  [
                'has_error'=>true,
                'message'=>'用户名或密码错误'
            ];
            $this->fetch($result);
        }
        $password = md5($password);
        $user = Db::table("amino_user")->where('username',$username)->where('password', $password)->find();
        if ($user!=null && count($user)>0){
            Session::set('username', $user['username']);
            Session::set('logining', 1);

            $result = [
                'has_error'=>false,
                'message'=>'登录成功',
                'username'=>$user['username']
            ];
            return json($result);
        }else{
            $result = [
                'has_error'=>true,
                'message'=>'用户名或密码错误'
            ];
            return json($result);
        }
    }

    public function logout(){
        if (Session::has('logining')){
            session_destroy();
            $result = [
                'has_error'=>false,
                'message'=>'成功退出'
            ];
        }else{
            $result = [
                'has_error'=>true,
                'message'=>'未登录，无需退出'
            ];
        }
        return json($result);
    }

     public function register(){
         $submit = input('submit');
         if (!is_null($submit)){
             $username = input('username');
             $password = input('password');
             $repassword = input('repassword');
			 $email = input('email');
			 $tel = input('tel');
             if (strlen($username)==0 || strlen($password)==0){
                 return $this->error('用户名或密码错误', url('user/register'));
             }

             if (strlen($repassword)==0){
                 return $this->error('请输入确认密码', url('user/register'));
             }

             if ($password!=$repassword){
                 return $this->error('两次密码输入不对', url('user/register'));
             }

             $user = Db::name('amino_user')->where('username', $username)->select();
             if ($user!=null && count($user)>0){
                 return $this->error('用户名已经存在，请重新注册', url('user/register'));
             }

             $data = [
                 'username'=>$username,
                 'password'=>md5($password),
				 'email'=>$email,
				 'tel'=>$tel
             ];

             $result_num = Db::table('amino_user')->insert($data);
             if ($result_num>0){
                 return $this->success('成功注册，请登录后使用', url('index/index'));
             }else{
                 return $this->error('注册失败，请重新注册', url('user/register'));
             }
         }
          return $this->fetch();
     }
}